package com.szalay.desktopsearch.ui;

import com.szalay.desktopsearch.indexer.Indexer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import org.apache.solr.client.solrj.SolrServerException;

/**
 * The statistics panel.
 */
final class StatisticsPanel extends JPanel {
    private static final long serialVersionUID = -2113401560807242199L;
    private final Indexer indexer;
    
    /**
     * Create a new statistics panel.
     */
    StatisticsPanel(final Indexer indexer) throws SolrServerException {
        setLayout(new SpringLayout());
        this.indexer = indexer;
    }
    
    @Override
    public void repaint() {
        removeAll();
        try {
            final JLabel numDocsLabel = new JLabel("<html><b>Number of documents</b></html>");
            final JLabel numDocsValue = new JLabel(Long.valueOf(indexer.getNumberOfDocumentsIndexed()).toString());
            add(numDocsLabel);
            add(numDocsValue);
            
            final JLabel nextFireTimeLabel = new JLabel("<html><b>Next indexing time</b></html>");
            final JLabel nextFireTimeValue = new JLabel(formatDate(indexer.getNextIndexingTime()));
            add(nextFireTimeLabel);
            add(nextFireTimeValue);
            
            
            //Layout
            UI.makeCompactGrid(this, 2, 2, 5, 5, 10, 10);
        }
        catch (Exception e) {
            //Nothing...
        }
    }
    
    private String formatDate(final Date date) {
        if (date == null) {
            return "";
        }
        final SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm", Locale.ENGLISH);
        return format.format(date);
    }
}
